package model.userModel;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Tool {

    public final static String url = "jdbc:mysql://43.142.87.68:3306/data_coffer?useUnicode=true&characterEncoding=utf8";
    public final static String name = "root";
    public final static String pwd = "123456";

    public static int DML(String sql, Object ... args) throws SQLException {
        try(final Connection c = DriverManager.getConnection(url, name, pwd)){
            try(final PreparedStatement pre = c.prepareStatement(sql)){
                for (int i = 0; i < args.length; i++) {
                    pre.setObject(i+1, args[i]);
                }
                return pre.executeUpdate();
            }
        }
    }

    public static List<Object []> DQL(String sql, Object ... args) throws SQLException{
        try(final Connection c = DriverManager.getConnection(url, name, pwd)){
            try(final PreparedStatement pre = c.prepareStatement(sql)){
                for (int i = 0; i < args.length; i++) {
                    pre.setObject(i+1, args[i]);
                }
                List<Object []> result = new ArrayList<>();
                ResultSet rs = pre.executeQuery();
                int clos = rs.getMetaData().getColumnCount();
                while (rs.next()){
                    Object [] row = new Object [clos];
                    for (int i = 0; i < clos; i++) {
                        row[i] = rs.getObject(i+1);
                    }
                    result.add(row);
                }
                return result;
            }
        }
    }

}
